package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.zuipin.entity.BaseProductBrand;
import com.zuipin.entity.HotReview;
import com.zuipin.entity.SysUser;
import com.zuipin.entity.product.CategoryInfo;
import com.zuipin.entity.product.ProductExtInfo;
import com.zuipin.entity.product.ProductInfo;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.HotReviewMapper;
import com.zuipin.service.IBaseProductBrandService;
import com.zuipin.service.ICategoryInfoService;
import com.zuipin.service.IHotReviewService;
import com.zuipin.service.IProductExtInfoService;
import com.zuipin.service.IProductInfoService;
import com.zuipin.service.IProductPriceService;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.HotReviewDetailVo;
import com.zuipin.vo.HotReviewProVo;
import com.zuipin.vo.ProductPriceVo;

/**
 * 热评
 * @author zhouhuihang
 *
 */
@Service
public class HotReviewServiceImpl implements IHotReviewService {
	@Resource
	private HotReviewMapper				hotReviewMapper;
	@Resource
	private IProductInfoService			productInfoService;
	@Resource
	private IProductExtInfoService		productExtInfoService;
	@Resource
	private ISysUserService				sysUserService;
	@Resource
	private IProductPriceService		productPriceService;
	@Resource
	private IBaseProductBrandService	baseProductBrandService;
	@Resource
	private ICategoryInfoService		categoryInfoService;
	
	/**
	 * 
	 * 分页查询热评（前后通用）
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result<List<HotReview>> searchHotReviewAllByBack(String productSku, String productName, Integer reviewStatus, Short isUseSort, Long sysId, Pagination page) {
		Long productId = null;
		String productIds = null;
		List<HotReview> data = new ArrayList<HotReview>();
		PageResult<List<HotReview>> result = new PageResult<List<HotReview>>();
		if (productSku != null) {
			// 精确查找SKU
			ProductInfo productInfo = productInfoService.selectByProductProSku(productSku);
			if (productInfo != null) {
				productId = productInfo.getId();
			} else {
				result.setData(data);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("查询成功");
				return result;
			}
		}
		if (productName != null) {
			// 模糊查找名字
			List<Long> listIds = productInfoService.findIdByName(productName);
			if (listIds != null && listIds.size() > 0) {
				productIds = StringUtils.collectionToDelimitedString(listIds, ",");
			}
		}
		data = hotReviewMapper.getHotReview(productId, productIds, reviewStatus, isUseSort, page);
		for (HotReview hotReview : data) {
			// 装配商品名称 商品SKU
			ProductInfo proInfo = productInfoService.findProductInfoById(hotReview.getProductId());
			if (proInfo != null) {
				hotReview.setProductName(proInfo.getProName());
				hotReview.setProductSku(proInfo.getProSku());
				BaseProductBrand brand = baseProductBrandService.findById(proInfo.getBrandId());
				// 装配净重
				hotReview.setNetWeight(proInfo.getNetWeight());
				// 装配品牌信息
				hotReview.setBrandId(proInfo.getBrandId());
				if (brand != null) {
					hotReview.setBrandName(brand.getBrandName());
				}
				CategoryInfo catInfo = categoryInfoService.selectByPrimaryKey(proInfo.getCatId());
				// 装配品类信息
				hotReview.setCatId(proInfo.getCatId());
				if (catInfo != null) {
					hotReview.setCatName(catInfo.getCatName());
				}
				// 查询商品扩展信息
				ProductExtInfo proExtInfo = productExtInfoService.findProductExtInfoBySku(proInfo.getProSku(), sysId);
				if (proExtInfo != null && ProductExtInfo.IS_SPECIAL.equals(proExtInfo.getIsSales())) {
					// 装配活动价格信息
					hotReview.setSalesPrice(proExtInfo.getSalesPrice());
				}
				// 醉品价格
				List<ProductPriceVo> prices = productPriceService.findByProductId(ProductPriceVo.ZUIPIN, proInfo.getId());
				for (ProductPriceVo productPrice : prices) {
					if (ProductPriceVo.PRICE_PLAT.equals(productPrice.getPriceType())) {
						hotReview.setPlatPrice(productPrice.getPrice());
					}
				}
			}
		}
		result.setTotal_record(page.getTotalRecord());
		result.setData(data);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("查询成功");
		return result;
	}
	
	/**
	 * 
	 * 新建热评
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result creatHotReview(HotReview hotReview) {
		Result result = new Result();
		HotReviewProVo hotReviewPro = productInfoService.getHotReviewProVo(hotReview.getProductId());
		if (hotReviewPro == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("商品不存在");
			return result;
		}
		if (HotReviewProVo.DISABLE.equals(hotReviewPro.getStatus())) {
			result.setErrorCode(ErrorCode.ELSE);
			result.setMessage("该商品己禁用，不可新增热评");
			return result;
		}
		List<HotReview> oldHotReview = hotReviewMapper.selectByProId(hotReviewPro.getId());
		if (oldHotReview != null && oldHotReview.size() > 0) {
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("该商品己添加热评，不可重复添加！");
			return result;
		}
		SysUser sysUser = sysUserService.findById(hotReview.getCreateId());
		if (sysUser != null) {
			hotReview.setCreator(sysUser.getRealName());
			hotReview.setOperator(sysUser.getRealName());
		}
		hotReview.setReviewStatus(HotReview.ENABLE);
		hotReview.setDelFlag(0);
		hotReview.setCreatedTime(DateUtils.getCurrentDateTime());
		hotReview.setOperatingTime(DateUtils.getCurrentDateTime());
		hotReviewMapper.insertSelective(hotReview);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("新建成功！");
		return result;
	}
	
	/**
	 * 
	 * 修改热评
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result updateHotReview(HotReview hotReview) {
		Result result = new Result();
		HotReview oldHotReview = hotReviewMapper.selectById(hotReview.getId());
		if (oldHotReview == null || oldHotReview.getDelFlag().equals(HotReview.DELFLAG_1)) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("没有查询到热评");
			return result;
		}
		HotReviewProVo hotReviewPro = productInfoService.getHotReviewProVo(hotReview.getProductId());
		if (hotReviewPro == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("商品不存在");
			return result;
		}
		if (HotReviewProVo.DISABLE.equals(hotReviewPro.getStatus())) {
			result.setErrorCode(ErrorCode.ELSE);
			result.setMessage("该商品己禁用，不可新增热评");
			return result;
		}
		List<HotReview> oldHotReviews = hotReviewMapper.selectByProId(hotReviewPro.getId());
		for (HotReview oldhotReview : oldHotReviews) {
			if (!hotReview.getId().equals(oldhotReview.getId())) {
				result.setErrorCode(ErrorCode.OBJECT_EXIST);
				result.setMessage("该商品己添加热评，不可重复添加！");
				return result;
			}
		}
		SysUser sysUser = sysUserService.findById(hotReview.getCreateId());
		if (sysUser != null) {
			hotReview.setOperator(sysUser.getRealName());
		}
		hotReview.setOperatingTime(DateUtils.getCurrentDateTime());
		hotReviewMapper.updateByBack(hotReview);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("修改成功！");
		return result;
	}
	
	/**
	 * 
	 * 查看热评详情
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result<HotReviewDetailVo> findHotReviewDetail(Long id) {
		HotReviewDetailVo hotReviewDetail = new HotReviewDetailVo();
		Result<HotReviewDetailVo> result = new Result<HotReviewDetailVo>();
		HotReview hotReview = hotReviewMapper.selectById(id);
		if (hotReview == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("没有查询到该热评信息！");
			return result;
		}
		BeanUtils.copyProperties(hotReview, hotReviewDetail);
		ProductInfo productInfo = productInfoService.findProductInfoById(hotReview.getProductId());
		if (productInfo == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("热评无效，商品不存在！");
			return result;
		}
		hotReviewDetail.setProductSku(productInfo.getProSku());
		hotReviewDetail.setProductName(productInfo.getProName());
		hotReviewDetail.setNetWeight(productInfo.getNetWeight());
		// 醉品价格
		List<ProductPriceVo> prices = productPriceService.findByProductId(ProductPriceVo.ZUIPIN, productInfo.getId());
		for (ProductPriceVo productPrice : prices) {
			if (ProductPriceVo.PRICE_PLAT.equals(productPrice.getPriceType())) {
				hotReviewDetail.setPrice(productPrice.getPrice());
			}
		}
		// 商品名称
		hotReviewDetail.setProductName(productInfo.getProName());
		result.setData(hotReviewDetail);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("查询成功");
		return result;
	}
	
	/**
	 * 
	 * 禁用|启用热评
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result modifyHotReviewStatus(Long id, Integer reviewStatus) {
		Result result = new Result();
		HotReview oldHotReview = hotReviewMapper.selectById(id);
		if (oldHotReview == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("热评不存在");
			return result;
		}
		if ((HotReview.DISABLE.equals(oldHotReview.getReviewStatus())) && (HotReview.ENABLE.equals(reviewStatus))) {
			hotReviewMapper.modifyHotReviewStatus(id, reviewStatus);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("启用成功");
			return result;
		}
		if ((HotReview.ENABLE.equals(oldHotReview.getReviewStatus())) && (HotReview.DISABLE.equals(reviewStatus))) {
			hotReviewMapper.modifyHotReviewStatus(id, reviewStatus);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("禁用成功");
			return result;
		}
		result.setErrorCode(ErrorCode.ELSE);
		result.setMessage("无法执行禁用/启用");
		return result;
	}
	
	/**
	 * 
	 * 查询热评商品基本信息
	 * 
	 */
	@Transactional(rollbackFor = { Exception.class })
	public Result<HotReviewProVo> getHotReviewPro(String productSku) {
		Result<HotReviewProVo> result = new Result<HotReviewProVo>();
		HotReviewProVo hotReviewPro = productInfoService.getHotReviewProVo(productSku);
		if (hotReviewPro == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("商品不存在");
			return result;
		}
		if (HotReviewProVo.DISABLE.equals(hotReviewPro.getStatus())) {
			result.setErrorCode(ErrorCode.ELSE);
			result.setMessage("该商品己禁用，不可新增热评");
			return result;
		}
		List<HotReview> oldHotReview = hotReviewMapper.selectByProId(hotReviewPro.getId());
		if (oldHotReview != null && oldHotReview.size() > 0) {
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("该商品己添加热评，不可重复添加！");
			return result;
		}
		// 醉品价格
		List<ProductPriceVo> prices = productPriceService.findByProductId(ProductPriceVo.ZUIPIN, hotReviewPro.getId());
		for (ProductPriceVo productPrice : prices) {
			if (ProductPriceVo.PRICE_PLAT.equals(productPrice.getPriceType())) {
				hotReviewPro.setPrice(productPrice.getPrice());
			}
		}
		result.setData(hotReviewPro);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("查询成功");
		return result;
	}
}
